package com.redhat.auditor.helper;

import java.io.PrintStream;

/**
 * Convenience class for logging messages, error stack traces and data to its
 * native console or log file. By default this class outputs to the JRE System
 * console via the System {@code PrintStream} ({@link System#out}). It can be
 * set to output to an alternative console or log file by by modifying the
 * output stream with the {@link #setOutputStream(PrintStream)} method. This
 * applies globally for the whole class and all invocations of its methods (not
 * just instances of it), across the entire Java Runtime.
 * <p>
 * Changing the underlying console will affect all further uses of the class,
 * and therefore arbitrary or or frequent changes are discouraged unless there
 * is a specific use case or requirement for outputting to multiple consoles. If
 * so, there should be strict management and synchronization of the output
 * stream to ensure that a program does not modify it unexpectedly whilst other
 * programs need to output to the original console. Allowing this to happen may
 * cause output messages from other programs to be sent to unintended or
 * unwanted locations.
 * <p>
 * In general, an alternative output stream should be set before any use of the
 * console, and only modified during usage if for example log files have moved
 * or logging is shifted to a new console.
 * 
 * @since 0.1
 * @version %I%, %G%
 * @author Trungtin Ton
 * 
 */
public class console {

	/**
	 * The underlying print stream that is used by this class's methods to write
	 * output to the target console or log file. This may be set to a different
	 * print stream via the {@link #setOutputStream} method.
	 */
	private static PrintStream out = System.out;

	/**
	 * @param args
	 */
	public static void log(final Object... args) {
		for (final Object arg : args)
			out.println(arg);
	}

	/**
	 * @param msg
	 */
	public static void error(final String msg) {
		new Error(msg).printStackTrace();
	}

	/**
	 * @param cause
	 */
	public static void error(final Throwable cause) {
		cause.printStackTrace();
	}

	/**
	 * @param out
	 */
	public static PrintStream setOutputStream(final PrintStream out) {
		final PrintStream previous = out;
		console.out = out;
		return previous;
	}
}